<?php
	$u_consultant = isset($_GET['consultant'])?$_GET['consultant']:'';
	//var_dump($contexts);exit;
?>

<?php $this->load->view('header');?>

<div class="page-container">
	<?php $this->load->view('sidebar');?>
	<!-- BEGIN CONTENT -->
	<div class="page-content-wrapper">
		<div class="page-content">
			<?php $this->load->view('sale/_sub_nav');?>
			<div class="row">
				<div class="col-md-12">
					<div class="table-toolbar">
						<div class="form-inline">
					 		<label>
								查看范围
								<select name="" id="clue_range_select" class="form-control">
									<option value="<?php echo site_url('/sale/receptions');?>">所有线索</option>
									<?php foreach ($consultants as $consultant) :?>
				                        <option value="<?php echo site_url('/sale/receptions?consultant='.$consultant['id']);?>" <?php echo $u_consultant==$consultant['id']?'selected="selected"':'';?>>
				                            <?php echo $consultant['nickname']; ?>的线索
				                        </option>
				                    <?php endforeach; ?>
								</select>
							</label> 
							<div class="pull-right">
								<a href="<?php echo site_url('/sale/clues_create');?>" class="btn blue" >
								 <i class="fa fa-plus"></i> 新建 
								</a> 
								<button id="clue_search_btn" class="btn yellow">
								 <i class="fa fa-search"></i> 搜索 
								</button> 
							</div>
						</div>
					</div>
						
						<div class="row" style="display:none;" id="clue_search_well">
							<div class="col-md-1"></div>
							<div class="col-md-10 well">
								<form>
									<table class="table">
										<tr><td colspan="6"><div class="well shallow-well">
											<span class="filter-link-list">
												按首字母来查询:
												<?php for($i=ord('a');$i<ord('z');$i++):?>
													 <a href="<?php echo site_url('/sale/reception?letter='.chr($i));?>"><?php echo strtoupper(chr($i));?></a>
												<?php endfor;?>		
											</span>
										</div></td>			
										</tr>
										<tr>
											<td><label><?php echo lang('clue_student_name');?></label></td>
											<td><input type="text" name="student_name" class="form-control"></td>
											<td><label><?php echo lang('clue_student_no');?></label></td>
											<td><input type="text" name="student_no" class="form-control"></td>
											<td><label><?php echo lang('clue_phone');?></label></td>
											<td><input type="text" name="phone" class="form-control"></td>						
										</tr>
										<tr>
											<td><label class="control-label"><?php echo lang('clue_last_contact_date');?></label></td>
											<td><div class="input-group input-large date-picker input-daterange" data-date="10/11/2012" data-date-format="mm/dd/yyyy">
												<input type="text" class="form-control" name="from">
												<span class="input-group-addon">
													 to
												</span>
												<input type="text" class="form-control" name="to">
											</div></td>
											<td><label class="control-label"><?php echo lang('clue_trade_wish');?></label></td>
											<td>
												<select class="form-control" name="trade_wish">
												     <option value=""></option>
													 <?php foreach(config_item('comm.trade_wish') as $key=>$value):?>
						                                <option value="<?php echo $key;?>"><?php echo $value;?></option>
						                            <?php endforeach;?>
												</select>
											</td>
											<td><label><?php echo lang('clue_consultant');?></label></td>
											<td>
												<select class="form-control" name="consultant">
													<option value=""></option>
						                                <?php if (isset($consultants) && is_array($consultants) && count($consultants)) :?>
						                                    <?php foreach ($consultants as $consultant) :?>
						                                         <option value="<?php echo $consultant['id']; ?>">
						                                            <?php echo $consultant['nickname']; ?>
						                                        </option>
						                                    <?php endforeach; ?>
						                                <?php endif;?>
												</select>
											</td>					
										</tr>
										<tr>
											<td><label class="control-label"><?php echo lang('clue_school');?></label></td>
											<td><input type="text" class="form-control"></td>					
											<td><label class="control-label"><?php echo lang('clue_grade');?></label></td>
											<td>
												<select class="form-control" name="grade">
												     <option value=""></option>
													 <?php foreach(config_item('comm.grade') as $key=>$value):?>
						                                <option value="<?php echo $key;?>"><?php echo $value;?></option>
						                            <?php endforeach;?>
												</select>
											</td>
											<td>
												<label class="control-label"><?php echo lang('clue_source');?></label>	
											</td>
											<td>
												<select class="form-control" name="clue_source">
												    <option value=""></option>
													 <?php foreach(config_item('comm.clue_source') as $key=>$value):?>
						                                <option value="<?php echo $key;?>"><?php echo $value;?></option>
						                            <?php endforeach;?>
												</select>
											</td>
										</tr>
										<tr>
											<td><label class="control-label"><?php echo lang('clue_next_track_date');?></label></td>
											<td><input type="text" class="form-control"></td>					
											<td><label class="control-label"><?php echo lang('from_big');?></label></td>
											<td>
												<select class="form-control" name="from_big">
													<option value=""></option>
													 <?php foreach(config_item('comm.from_big') as $key=>$value):?>
						                                <option value="<?php echo $key;?>"><?php echo $value;?></option>
						                            <?php endforeach;?>
												</select>
											</td>
											<td>
												<label class="control-label"><?php echo lang('clue_stat');?></label>	
											</td>
											<td>
												<select class="form-control" name="clue_stat">
													  <option value=""></option>
													 <?php foreach(config_item('comm.clue_status') as $key=>$value):?>
						                                <option value="<?php echo $key;?>"><?php echo $value;?></option>
						                            <?php endforeach;?>
												</select>
											</td>
										</tr>
										<tr>
											<td>
												<label class="control-label"><?php echo lang('clue_track_nums');?></label>	
											</td>
											<td>
												<select class="form-control" name="contact_times">
													  <option value=""></option>
													  <?php for($i=0;$i<6;$i++):?>
													  	 <option value="<?php echo $i;?>"><?php echo $i;?>次</option>
													  <?php endfor;?>
													  <option value="6">6-10次</option>
													  <option value="10">大于10次</option>
												</select>
											</td>
											<td>
											</td>
											<td>
											</td>
											<td>
											</td>
											<td>
											</td>
										</tr>					
										<tr>
											<td colspan="5"></td>
											<td><button class="btn btn-success"  id="search_submit_btn">查询</button> <button class="btn btn-default" id="search_reset_btn">重置</button></td>
										</tr>
									</table>
								</form>
							</div>
							<div class="col-sm-1"></div>	
						</div>


						<table class="table table-hover" id="clues_table">
							<thead>
								<tr>
									<th><?php echo lang('clue_student_name');?></th>
									<th><?php echo lang('clue_student_no');?></th>
									<th><?php echo lang('clue_trade_wish');?></th>
									<th><?php echo lang('clue_phone');?></th>
									<th><?php echo lang('clue_consultant');?></th>
									<th><?php echo lang('clue_last_contact_result');?></th>
									<th><?php echo lang('clue_last_contact_date');?></th>
									<th><?php echo lang('clue_next_contact_date');?></th>
									<th><?php echo lang('bf_actions');?></th>
								</tr>
							</thead>
							<tbody>
								<?php if($clues):?>
								<?php foreach ($clues as $key => $clue):?>
								<tr>
									<td><a href="<?php echo site_url('/sale/clues/view/'.$clue->student_id);?>"><?php echo $clue->student_name;?></a></td>
									<td>D-<?php echo sprintf('%06d',$clue->student_id);?></td>
									<td><?php 
									$trade_wishs = config_item('comm.trade_wish');
									echo $trade_wishs[($clue->trade_wish)];
									?></td>
									<td><?php echo $clue->phone;?></td>
									<td><?php echo $clue->nickname;?></td>
									<td><?php echo $clue->last_contact_result;?></td>
									<td><?php echo $clue->next_contact_date;?></td>
									<td><?php echo $clue->next_contact_date;?></td>
									<td>
										<?php if(empty($clue->status)):?>
											<button class="btn btn-xs btn-primary clue_visit_btn" data-pk="<?php echo $clue->student_id;?>">上门确认</button>
										<?php else:?>
											<button class="btn btn-xs default " disabled="">已上门</button>
										<?php endif;?>
											<button class="btn btn-xs btn-warning clue_dist_btn" data-target="#dist_modal" data-toggle="modal" data-pk="<?php echo $clue->student_id;?>">分配</button>
									</td>
								</tr>
								<?php endforeach;?>
								<?php endif;?>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
	</div>
	
	<div class="modal fade" id="dist_modal" role="dialog" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4 class="modal-title">分配/转移</h4>
				</div>
				<div class="modal-body">
					<form action="" method="POST" class="form-horizontal" role="form">
						<input type="hidden" name="clue_ids" id="clue_ids" />												
						<div class="form-group">
							<label class="control-label col-md-4">转移拥有关系</label>
							<div class="col-sm-6">
								<select name="" id="new_owner" class="form-control" required="required">
									<?php foreach ($consultants as $consultant) :?>
				                        <option value="<?php echo $consultant['id']; ?>">
				                            <?php echo $consultant['nickname']; ?>的线索
				                        </option>
				                    <?php endforeach; ?>
								</select>
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary" id="dist_submit_btn">分配/转移</button>
				</div>
			</div><!-- /.modal-content -->
		</div><!-- /.modal-dialog -->
	</div><!-- /.modal -->

	<div class="modal fade" id="track_modal" role="dialog" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4 class="modal-title"><?php echo lang('clue_new_track');?></h4>
				</div>
				<div class="modal-body">
					<?php echo form_open($this->uri->uri_string(), array('class'=>'form-horizontal','role'=>'form')); ?>
<!-- 				        <h3 class="form-section"><?php echo lang('clue_detail'); ?></h3>
 -->		            <div class="form-group">
		                    <label class="control-label col-md-3" ><?php echo lang('clue_student_name_no'); ?></label>
		                    <div class="col-md-9">
		                        <input type="text" name="" id="student_name" readonly class="form-control" value=" " />
		                        <input type="hidden" name="student_id" id="student_id" value="1">
		                        <span class="help-block"></span>
		                    </div>
		                </div>

 						<div class="form-group<?php echo form_error('title') ? ' error' : ''; ?>">
		                    <label class="control-label col-md-3" ><?php echo lang('clue_track_title'); ?></label>
		                    <div class="col-md-9">
		                        <input type="text" name="title" id="title" class="form-control" value="<?php echo set_value('role_name', isset($role) ? $role->role_name : ''); ?>" />
		                        <span class="help-block"><?php echo form_error('title'); ?></span>
		                    </div>
		                </div>

		                <div class="form-group<?php echo form_error('track_wish') ? ' error' : ''; ?>">
		                    <label class="control-label col-md-3" ><?php echo lang('clue_trade_wish'); ?></label>
		                    <div class="col-md-5">
		                        <select name="track_wish" id="track_wish" class="form-control" >
		                            <?php foreach(config_item('comm.trade_wish') as $key=>$value):?>
		                                <option value="<?php echo $key;?>" <?php echo set_select('clue_source', $key); ?>><?php echo $value;?></option>
		                            <?php endforeach;?>
		                        </select>
		                        <span class="help-block"><?php echo form_error('track_wish'); ?></span>
		                    </div>
		                </div>

		                <div class="form-group<?php echo form_error('track_way') ? ' error' : ''; ?>">
		                    <label class="control-label col-md-3" ><?php echo lang('clue_track_way'); ?></label>
		                    <div class="col-md-5">
		                        <select class="form-control" name="track_way" id="track_way">
		                        	 <?php foreach(config_item('comm.track_way') as $key=>$value):?>
		                                <option value="<?php echo $key;?>" <?php echo set_select('track_way', $key); ?>><?php echo $value;?></option>
		                            <?php endforeach;?>
		                        </select>
		                        <span class="help-block"><?php echo form_error('track_way'); ?></span>
		                    </div>
		                </div>


		                <div class="form-group<?php echo form_error('track_date') ? ' error' : ''; ?>">
		                    <label class="control-label col-md-3" ><?php echo lang('clue_track_date'); ?></label>
		                    <div class="col-md-5">
		                    	<div class="input-group date date-picker">
			                        <input type="text" name="track_date" id="track_datee" class="form-control" value="<?php echo set_value('role_name', isset($role) ? $role->role_name : ''); ?>" />
			                        <span class="input-group-btn">
										<button class="btn default" type="button"><i class="fa fa-calendar"></i></button>
									</span>
								</div>
		                        <span class="help-block"><?php echo form_error('track_date'); ?></span>
		                    </div>
		                </div>

		                <div class="form-group<?php echo form_error('next_track_date') ? ' error' : ''; ?>">
		                    <label class="control-label col-md-3" ><?php echo lang('clue_next_track_date'); ?></label>
		                    <div class="col-md-5">
		                    	<div class="input-group date date-picker">
			                        <input type="text" name="next_track_date" id="track_next_date" class="form-control" value="<?php echo set_value('next_track_date', isset($role) ? $role->role_name : ''); ?>" />
			                        <span class="input-group-btn">
										<button class="btn default" type="button"><i class="fa fa-calendar"></i></button>
									</span>
								</div>
		                        <span class="help-block"><?php echo form_error('next_track_date'); ?></span>
		                    </div>
		                </div>

		                <div class="form-group<?php echo form_error('content') ? ' error' : ''; ?>">
		                    <label class="control-label col-md-3" ><?php echo lang('clue_track_content'); ?></label>
		                    <div class="col-md-5">
		                        <textarea name="content" class="form-control"></textarea>
		                        <span class="help-block"><?php echo form_error('content'); ?></span>
		                    </div>
		                </div>				     			

		                 <div class="form-group<?php echo form_error('user_id') ? ' error' : ''; ?>">
		                    <label class="control-label col-md-3" ><?php echo lang('clue_consultant'); ?></label>
		                    <div class="col-md-5">
		                        <select class="form-control" name="consultant" id="consultant">
		                                <?php if (isset($consultants) && is_array($consultants) && count($consultants)) :?>
		                                    <?php foreach ($consultants as $consultant) :?>
		                                         <option value="<?php echo $consultant['id']; ?>">
		                                            <?php echo $consultant['nickname']; ?>
		                                        </option>
		                                    <?php endforeach; ?>
		                                <?php else:?>
		                                    <option value="0">无</option>
		                                <?php endif;?>
		                        </select>
		                        <span class="help-block"><?php echo form_error('user_id'); ?></span>
		                    </div>
		                </div>				     			
	
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal"><?php echo lang('bf_action_cancel');?></button>
					<button type="submit" class="btn btn-primary" name="track"><?php echo lang('bf_action_save');?></button>
				</div>

				<?php echo form_close();?>
			</div><!-- /.modal-content -->
		</div><!-- /.modal-dialog -->
	</div><!-- /.modal -->

<?php $this->load->view('footer');?>


<script>
jQuery(document).ready(function() {
   App.init(); // initlayout and core plugins

    // 初始化
    var table = $('#clues_table').dataTable();

    // 切换显示
    $(document).on('click','#clue_search_btn',function(){
        $('#clue_search_well').toggle();
    });

    //上门确认
	$(document).on('click','.clue_visit_btn',function(){
	    var id = $(this).attr('data-pk');
	    var tr = $(this).parent().parent();
	    if(id) 
	    {
	        bootbox.confirm("是否已经上门?", function(result) {
	            if(result)
	            {
	                $.ajax({
	                    url: 'sale/do_visit',
	                    type:'post',
	                    dataType: 'json',
	                    data:{'id':id}
	                }).always(function () {
	                }).done(function (response) {
	                	if(response.code ==0){
	                		toastr.success(response.msg);
	                		tr.remove();
	                	} else {
	                		toastr.error(response.msg);
	                	}
	                    //window.location.reload();
	                });
	            }
	        });
	    }
	});

	//分配线索
	$(document).on('click','.clue_dist_btn',function(){
	    var id = $(this).attr('data-pk');
	    console.log(id);
	    $('#dist_modal').on('shown.bs.modal', function (e) {        
	        $("#dist_modal #clue_ids").val(id);
	    });
	});

	//转移线索
	jQuery("#clue_dist_btn").on('click',function(){
	    var ids = get_check_value();
	    console.log(ids);
	    if(ids !="")
	    {
	        $("#dist_modal #clue_ids").val(ids);
	    } else {
	        alert("请至少选择一条记录");
	        return false;
	    }
	});


	//转移线索提交
	jQuery("#dist_submit_btn").on('click',function(){
	    var ids = $("#clue_ids").val();
	    var new_owner = $("#new_owner").val();
	    // alert(ids);
	    if (ids && new_owner)
	    {
	        $.ajax({
	            url: 'sale/do_dist',
	            type:'post',
	            dataType: 'json',
	            data:{'ids':ids,'owner':new_owner}
	        }).always(function () {

	        }).done(function (response) {
				if(response.code ==0){
            		toastr.success(response.msg);
            		tr.remove();
            	} else {
            		toastr.error(response.msg);
            	}
	        });
	    }
	});
});
</script>

</body>
<!-- END BODY -->
</html>

